define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'good/index' + location.search,
                    add_url: 'good/add',
                    edit_url: 'good/edit',
                    del_url: 'good/del',
                    multi_url: 'good/multi',
                    import_url: 'good/import',
                    table: 'good',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                $('ul.nav-tabs li a[data-value="1"]').trigger('click');
                $(".form-commonsearch select[name=status]").val("1");
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                showSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'gymnasium_id', title: __('Gymnasium_id')},
                        // {field: 'coach_id', title: __('Coach_id')},
                        // {field: 'gymnasium.name', title: __('Gymnasium.name')},
                        // {field: 'gymnasium.name', title: __('Gymnasium_id')},
                        // {field: 'coach.name', title: __('Coach.name')},
                        {field: 'type', title: __('Type'), searchList: {"1": __('Type 1'), "2": __('Type 2'), "3": __('Type 3'), "4": __('Type 4'), "5": __('Type 5')}, formatter: Table.api.formatter.normal},
                        {field: 'is_discount', title: __('是否优惠'), searchList: {"1":__('是'),"0":__('否'),}, formatter: Table.api.formatter.normal},
                        {field: 'stock', title: __('库存')},
                        {field: 'coach.name', title: __('Coach_id')},
                        {field: 'end_number', title: __('End_number'), operate: false},
                        {field: 'name', title: __('Name')},
                        // {field: 'title', title: __('Title'),operate:false},
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {field: 'thumb_image', title: __('Thumb_image'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        // {field: 'full_image', title: __('Full_image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'content', title: __('Content')},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0": __('Status 0'), "1": __('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            var coach = $("#coach_id");
            var number = $("#end_number");

            $('#c-type').on('change', function () {
                var val = Number($(this).val());
                if (val === 2) {
                    $('form[role=form]').validator("setField", 'row[coach_id]', "required");
                    coach.show();
                } else {
                    $('form[role=form]').validator("setField", 'row[coach_id]', null);
                    coach.hide();
                }
                if (val > 2) {
                    $('form[role=form]').validator("setField", 'row[end_number]', null);
                    number.hide();
                } else {
                    $('form[role=form]').validator("setField", 'row[end_number]', "required,range(0~),integer(+)");
                    number.show();
                }
            });

            Controller.api.bindevent();
        },
        edit: function () {
            var coach = $("#coach_id");
            var number = $("#end_number");
            // $('form[role=form]').validator("setField", 'row[coach_id]',null);
            $('#c-type').on('change', function () {
                var val = Number($(this).val());

                // 次卡 隐藏教练 显示次卡
                if(val===1){
                    $('form[role=form]').validator("setField", 'row[end_number]', "required,range(0~),integer(+)");
                    $('form[role=form]').validator("setField", 'row[coach_id]', null);
                    number.show();
                    coach.hide();
                }

                // 教练卡 显示教练 显示次卡
                if (val === 2) {
                    $('form[role=form]').validator("setField", 'row[end_number]', "required,range(0~),integer(+)");
                    $('form[role=form]').validator("setField", 'row[coach_id]', "required,range(0~),integer(+)");
                    number.show();
                    coach.show();
                }

                // 不为次卡 也不为教练卡 隐藏教练卡 隐藏 次卡
                if (val > 2) {
                    number.hide();
                    coach.hide();
                    $('form[role=form]').validator("setField", 'row[coach_id]', null);
                    $('form[role=form]').validator("setField", 'row[end_number]', null);
                }
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});